package tools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

@WebServlet("/changeMessageServlet")
public class changeMessageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String so = new String(request.getParameter("so").getBytes("ISO-8859-1"), "UTF-8");
        String message_id = new String(request.getParameter("message_id").getBytes("ISO-8859-1"), "UTF-8");
        System.out.println("到这里了");
        try {
            Connection con = null;
            Statement stmt = null;
            Class.forName("org.h2.Driver");
            /*url后面加的?useUnicode=true&characterEncoding=gbk，是为了处理向数据库中添加数据时出现乱码的问题。*/
            String url = "jdbc:h2:~/test1";
            //System.out.println("到达");
            con = DriverManager.getConnection(url, "root", "root");
            //System.out.println("到达2");
            HttpSession session = request.getSession();
            stmt=con.createStatement();
            //当key为1的时候修改配送状态
            //UPDATE "ORDER" SET STATE='已支付' WHERE ORDER_ID='33001'
            String sql= "UPDATE \"GOOD_MESSAGE\" SET so='"+so+"' WHERE message_id='"+message_id+"'";
            stmt.executeUpdate(sql);
            System.out.println("删除成功");
            stmt.close();
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        //跳转
        response.sendRedirect("http://localhost:8080/message_manageServlet");
        return;
    }
}
